/**
 * Created by kosmoknot on 8/19/2014.
 */

/**
 * Created by kosmoknot on 8/19/2014.
 */

function onCloudLabelMouseEvent ( event )
{
    if ( event.type == "click")
    {
        var label = this.getChildAt(1);

        window.location.href = "#/topics/" + selectedCategoryUuid + "&&" + label.text + "&&" + "";
    }
}
function createCloud (  canvas, data, fontStyles, sizeArray ) {

    var labelFontSizes;
    if ( sizeArray != undefined)
    {
        labelFontSizes = sizeArray;
    } else {
        labelFontSizes = [ 10, 15, 20, 25, 30 ];
    }
    var fontFamily = "Arial";
    var fontColor = "#333333";

    if ( fontStyles != undefined )
    {
        if ( fontStyles.length > 0 )
        {
            fontFamily = fontStyles[0];
        }
        if ( fontStyles.length > 1 )
        {
            fontColor = fontStyles[1];
        }
    }

    var stage = new createjs.Stage(canvas);
    var cloudContainer = new createjs.Container();
    stage.addChild ( cloudContainer );

    var tagCloudMin = 0;
    var tagCloudMax = 0;
    var tagCloudStep = 0;

    for ( var i = 0; i< data.length; i++)
    {
        item = data[i];
        if ( item.count != null )
        {
            tagCloudMin = Math.min( tagCloudMin, item.count );
            tagCloudMax = Math.max( tagCloudMax, item.count );
        }
    }

    tagCloudStep = Math.floor( ( tagCloudMax - tagCloudMin )/labelFontSizes.length );

    var maxRowWidth = 0;
    var rowWidth = 0
    var rowY = 20;
    var rowContainer = new createjs.Container();
    for ( i = 0; i< data.length; i++) {
        item = data[i];
        var index = Math.floor(item.count / tagCloudStep);

        if ( item.name == null )
            continue;

        var labelContainer = new createjs.Container();
        var rectangle = new createjs.Shape();
        var lw, lh

        var label = new createjs.Text(item.name.toUpperCase(), labelFontSizes[index] + "px " + fontFamily, fontColor);
        label.textAlign = "left";
        labelContainer.addChild ( label );
        labelContainer.on("click", onCloudLabelMouseEvent);
        labelContainer.on("dblclick", onCloudLabelMouseEvent);
        labelContainer.on("mouseover", onCloudLabelMouseEvent);
        labelContainer.on("mouseout", onCloudLabelMouseEvent);

        lw = label.getMeasuredWidth();
        lh = label.getMeasuredHeight();

        rectangle.graphics.beginStroke("rgba(255,0,0,.01)").beginFill("rgba(255,0,0,.01)").drawRect( 0,0,lw,lh );
        rectangle.graphics.endFill();
        labelContainer.addChildAt ( rectangle,0);

        labelContainer.width = lw;
        labelContainer.height = lh;

        if ( rowWidth + label.getMeasuredWidth() < canvas.width )
        {
            labelContainer.y += ( 30 - lh )/2;
            labelContainer.x = rowWidth + 20;
            rowWidth += lw + 20;

            rowContainer.addChild(labelContainer);
        } else {
            rowContainer.x = ( canvas.width - rowWidth )/2;
            cloudContainer.addChild(rowContainer);

            rowY += 30;
            rowWidth = 0;
            label.x = rowWidth;
            label.y += ( 30 - lh )/2;
            rowContainer = new createjs.Container();
            rowContainer.y = rowY;

            rowContainer.addChild(labelContainer);

            rowWidth += lw + 20;
        }

        rowContainer.x = ( canvas.width - rowWidth )/2;
        cloudContainer.addChild(rowContainer);
    }

    rowY += 30;

    cloudContainer.y = ( canvas.height - rowY )/2;

    stage.update();

}